package com.rw.admin.controller;

import com.github.pagehelper.PageInfo;
import com.rw.common.pojo.ConfigInfo;
import com.rw.common.pojo.Menu;
import com.rw.admin.service.ConfigInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * @author rabit
 */
@RestController
@RequestMapping("configInfo")
public class ConfigInfoController {
    @Autowired
    private ConfigInfoService configInfoService;
    @GetMapping("{key}")
    public Map<String, Object> getValueByKey(@PathVariable("key") String key){
        Map<String, Object> map =new HashMap<>();
        map.put("code",200);
        String value = configInfoService.getValueByKey(key.trim());
        if (value!=null){
            map.put("data",value);
        }else {
            map.put("code",199);
            map.put("msg","key不存在");
        }
        return map;
    }

    @GetMapping("list")
    public Map<String, Object> getDictByPage(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer limit,ConfigInfo configInfo){
        Map<String, Object> map =new HashMap<>();

        if (pageNum== null || pageNum < 1){
            pageNum = 1;
        }
        if (limit== null ||limit < 1){
            limit = 15;
        }
        PageInfo page = configInfoService.getDataByPage(pageNum, limit,configInfo);
        map.put("code",0);
        map.put("msg","");
        map.put("count",page.getTotal());
        map.put("data",page.getList());
        return map;
    }

    @PostMapping("save")
    public Map<String, Object> save(ConfigInfo configInfo){
        Map<String, Object> map =new HashMap<>();
        boolean flag=configInfoService.save(configInfo);
        if (flag){
            map.put("code",0);
            map.put("msg","保存成功");
        }else {
            map.put("code",199);
            map.put("msg","保存失败");
        }
        return map;
    }

    @PostMapping("update")
    public Map<String, Object> update(ConfigInfo ConfigInfo){
        Map<String, Object> map =new HashMap<>();
        boolean flag=configInfoService.update(ConfigInfo);
        if (flag){
            map.put("code",0);
            map.put("msg","修改成功");
        }else {
            map.put("code",199);
            map.put("msg","修改失败");
        }
        return map;

    }

}
